﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using SageKing.Core.Contracts;

namespace SageKing.UI.Razor.Base;



public class JsCommJsInterop : IAsyncDisposable
{
    private readonly Lazy<Task<IJSObjectReference>> moduleTask;
    public volatile bool IsDisposed = false;

    public JsCommJsInterop(IJSRuntime jsRuntime)
    {
        moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>(
            "import", $"./_content/SageKing.UI.Razor.Base/Components/Pages/JsCollocation/JsComm.razor.js?v={Environment.TickCount}").AsTask());
    }

    public async ValueTask<string> Prompt(string message)
    {
        var module = await moduleTask.Value;
        return await module.InvokeAsync<string>("showPrompt", message);
    }

    /// <summary>
    /// 检查浏览器是否支持必要的 APIs
    /// </summary>
    /// <returns></returns>
    public async ValueTask<bool> CheckClipboardSupport()
    {
        var module = await moduleTask.Value;

        var isSupported = await module.InvokeAsync<bool>("checkClipboardSupport");

        return isSupported;
    }

    /// <summary>
    /// 尝试将焦点重新设置到ele上
    /// </summary>
    /// <param name="copyRef"></param>
    /// <returns></returns>
    public async ValueTask BlazorFocusElement(ElementReference copyRef)
    {
        var module = await moduleTask.Value;

        // 尝试将焦点重新设置到ele上
        await module!.InvokeVoidAsync("BlazorFocusElement", copyRef);
    }
    /// <summary>
    /// 复制文本到剪贴板
    /// </summary>
    /// <param name="copy"></param>
    /// <returns></returns>
    public async ValueTask<bool> BlazorClipboardCopy(string copy)
    {
        var module = await moduleTask.Value;

        bool success = await module!.InvokeAsync<bool>("blazorClipboardCopy", copy);

        await Task.Delay(50); // 50ms 延迟通常足够

        return success;
    }
    /// <summary>
    /// 复制文本到剪贴板
    /// </summary>
    /// <param name="copyRef"></param>
    /// <param name="copy"></param>
    /// <returns></returns>
    public async ValueTask<bool> BlazorClipboardCopy(ElementReference copyRef, string copy)
    {
        var module = await moduleTask.Value;

        // 尝试将焦点重新设置到ele上
        await module!.InvokeVoidAsync("BlazorFocusElement", copyRef);

        // 短暂延迟后再尝试复制，以确保焦点已切换
        await Task.Delay(50); // 50ms 延迟通常足够

        bool success = await module!.InvokeAsync<bool>("blazorClipboardCopy", copy);

        return success;
    }
    public async ValueTask SetTextAndSelect(ElementReference copyRef, string copy)
    {
        var module = await moduleTask.Value;

        await module.InvokeVoidAsync("setTextAndSelect", copyRef, copy);

    }

    public async ValueTask<SKWindowSize> GetWindowSize()
    {
        if (IsDisposed)
        {
            return BrowserConsts.WindowSize;
        }
        var module = await moduleTask.Value;
        var windowSize = await module.InvokeAsync<SKWindowSize>("windowSize");
        return windowSize;
    }

    public async ValueTask DisposeAsync()
    {
        try
        {
            IsDisposed = true;

            if (moduleTask.IsValueCreated)
            {
                var module = await moduleTask.Value;
                await module.DisposeAsync();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}